home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / ppp / dp-2.3 / modem / setmodem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-19  |  4.5 KB  |  209 lines

  1. /*
  2.  * Copyright (c) 1992 Purdue University
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by Purdue University.  The name of the University may not be used
  11.  * to endorse or promote products derived * from this software without
  12.  * specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * Note: this copyright applies to portions of this software developed
  18.  * at Purdue beyond the software covered by the original copyright.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <malloc.h>
  23. #include <fcntl.h>
  24. #include <termios.h>
  25. #ifdef    STS
  26. #include <sys/ctio.h>
  27. #endif    STS
  28.  
  29. char *argv0;
  30. int verbose;
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     char *dev;
  37.     char *file;
  38.     int baud;
  39.     argv0 = argv[0];
  40.     if (argc < 3) {
  41.     fprintf(stderr, "usage: %s [-v] scriptfile device [baud]\n", argv0);
  42.     exit(33);
  43.     }
  44.     if (strcmp(argv[1], "-v") == 0) {
  45.     verbose++;
  46.     argv++;
  47.     argc--;
  48.     }
  49.     file = argv[1];
  50.     if (argv[2][0] == '/')
  51.     dev = argv[1];
  52.     else {
  53.     dev = malloc(strlen(argv[2])+sizeof("/dev/"));
  54.     (void)sprintf(dev, "/dev/%s", argv[2]);
  55.     }
  56.     baud = argc > 3 ? atoi(argv[3]) : 38400;
  57.     setmodem(dev, file, baud);
  58.     exit(0);
  59. }
  60.  
  61. setmodem(dev, file, baud)
  62. char *dev, *file;
  63. int baud;
  64. {
  65.     FILE *od, *id, *f;
  66.     char lnbuf[256], *l;
  67.     int d;
  68.     int osoft, soft;
  69.  
  70.     if ((d = open(dev, O_RDWR|O_NDELAY, 0)) < 0) {
  71.     perror(dev);
  72.     exit(34);
  73.     }
  74.     if (!(id = fdopen(d, "r"))) {
  75.     perror(dev);
  76.     exit(34);
  77.     }
  78.     if (!(od = fdopen(d, "w"))) {
  79.     perror(dev);
  80.     exit(34);
  81.     }
  82.     if (!(f = fopen(file, "r"))) {
  83.     perror(file);
  84.     exit(35);
  85.     }
  86.     if (ioctl(d, TIOCGSOFTCAR, &osoft) < 0)
  87.     perror("TIOCGSOFTCAR");
  88.     soft = osoft;
  89.     if (!osoft) {
  90.     soft = 1;
  91.     if (ioctl(d, TIOCSSOFTCAR, &soft) < 0)
  92.         perror("TIOCSSOFTCAR(1)");
  93.     }
  94.     setbaud(d, baud);
  95.     sendcom(od, id, "AT");
  96.     while (fgets(lnbuf, sizeof(lnbuf), f)) {
  97.     l = lnbuf + strlen(lnbuf) - 1;
  98.     if (*l = '\n')
  99.         *l = '\0';
  100.     sendcom(od, id, lnbuf);
  101.     }
  102.     if (osoft != soft) {
  103.     soft = 0;
  104.     if (ioctl(d, TIOCSSOFTCAR, &soft) < 0)
  105.         perror("TIOCSSOFTCAR(0)");
  106.     }
  107. }
  108. #include <errno.h>
  109.  
  110. #define    TIMEOUT 3
  111.  
  112. sendcom(o, i, com)
  113. FILE *o, *i;
  114. char *com;
  115. {
  116.     char ln[128], *l;
  117.     int c;
  118.     if (*com == '#') {
  119.     if (verbose)
  120.         fprintf(stderr, "Setting baud rate to: %s\n", com+1);
  121.     setbaud(fileno(o), atoi(com+1));
  122.     return;
  123.     }
  124.     if (verbose)
  125.     fprintf(stderr, "Send: %s\n", com);
  126.     fputs(com, o);
  127.     putc('\r', o);
  128.     fflush(o);
  129.     errno = 0;
  130. #if    1
  131.     while (fgets(ln, sizeof(ln), i)) {
  132.     l = ln + strlen(ln) - 1;
  133.     while (l >= ln && (*l == '\n' || *l == '\r'))
  134.         *l-- = '\0';
  135.     if (verbose && ln[0])
  136.         fprintf(stderr, "Recv: %s\n", ln);
  137.     if (strcmp(ln, "OK") == 0)
  138.         return;
  139.     }
  140.     if (errno) {
  141.     fprintf(stderr, "%s: ", argv0);
  142.     perror("Error reading from modem");
  143.     }
  144.     else
  145.     fprintf(stderr, "%s: modem timeout\n", argv0);
  146.     exit(38);
  147. #else
  148.     for (l = ln ; l < ln + sizeof(ln)-1 ; l++) {
  149.     if ((c = getc(i)) == EOF) {
  150.         perror("Error reading from modem");
  151.         exit(38);
  152.     }
  153. #if    1
  154.     fprintf(stderr, "getc %d '%c'\n", c, c);
  155. #endif
  156.     if (!(c == '\r' || c == '\n')) {
  157.         *l++ = c;
  158.         continue;
  159.     }
  160.     *l = '\0';
  161.     if (strcmp(ln, "OK") == 0)
  162.         return;
  163.     l = ln;
  164.     }
  165.     perror("Error reading from modem");
  166.     exit(39);
  167. #endif
  168. }
  169.  
  170.  
  171. setbaud(d, baud)
  172. int d, baud;
  173. {
  174.     int b;
  175.     struct termios t;
  176.     int baudext = 0;
  177.     switch (baud) {
  178.      case 300:     b = B300; break;
  179.      case 1200:  b = B1200; break;
  180.      case 9600:  b = B9600; break;
  181.      case 19200: b = B19200; break;
  182.      default:
  183.          fprintf(stderr,
  184.             "Invalid baud rate %d, setting baud rate to 38400\n",
  185.             baud);
  186.      case 38400: b = B38400; break;
  187. #ifdef    STS
  188.      case 57600: b = B38400; baudext = 1; break;
  189. #endif    STS
  190.     }
  191.     if (tcgetattr(d, &t) < 0) {
  192.     perror("tcgetattr");
  193.     exit(36);
  194.     }
  195.     cfsetispeed(&t, b);
  196.     cfsetospeed(&t, b);
  197.     t.c_oflag &= ~OPOST;
  198.     t.c_lflag &= ~(ICANON|ISIG);
  199.     t.c_cc[VTIME] = TIMEOUT*10;
  200.     t.c_cc[VMIN] = 0;
  201.     if (tcsetattr(d, TCSAFLUSH, &t) < 0) {
  202.     perror("tcsetattr");
  203.     exit(36);
  204.     }
  205. #ifdef    STS
  206.     (void)ioctl(d, STSBAUDEXTEN, &baudext);
  207. #endif  STS
  208. }
  209.